﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using xKnight.Analyzing.EncodedXss;
using xKnight.Analyzing.SimpleXss;
using xKnight.Analyzing.Xss;
using xKnight.Data;
using xKnight.Models;

namespace xKnight.Analyzing.EncodedXss
{
    public class EncodedXssAnalyzer : SimpleXssAnalyzer
    {

        #region Constructors

        public EncodedXssAnalyzer(Attack attack)
            :base(attack)
        {
            
        }

        #endregion

        #region Interface

        public override void Analyze()
        {
            XAttack xAttack=null;
            while ((xAttack = GetNotAnalyzedAttack()) != null)
            {
                XAttackParam[] attackParams = DataLayer.GetAttackParams(xAttack); 
                string response = xAttack.ResponsePage;
                xAttack.Succeed = HasReflectedVulnerablity(attackParams,response);

                EncodedXssAnalyzeAnnounceItem announceItem = new EncodedXssAnalyzeAnnounceItem(xAttack.Form.Action, xAttack.Form.Method, xAttack, "", DateTime.Now
                    , xAttack.Succeed.Value, DataLayer.GetWebPage(xAttack.Form.Id).Url);
                this.OnAnalyzeAnnounced(announceItem);

                DataLayer.Save(xAttack);
            }
        }

        #endregion

        #region Private Methods

        protected override XAttack GetNotAnalyzedAttack()
        {
            XAttack xAttack = null;

            lock (SharedResource.SharedLock)
            {
                xAttack = SharedResource.SharedQueue.Count != 0 ? SharedResource.SharedQueue.Dequeue() : null;
            }

            return xAttack;
        }

        #endregion
    }
}
